import java.util.Arrays;


public class MemoryManagementSystem_old {
	private String[] secondaryMemory;	//This array will hold the secondary memory
	private boolean useLRU; 				// true if LRU is used, false if FIFO is used
	private String[] mainMemory;
	private int[][] index;
	private int[] pageindex;
	private int pointer;
	public MemoryManagementSystem_old(boolean useLRU) {
		secondaryMemory = new String[1000];
		pageindex = new int[1000];
		for(int i=0;i<secondaryMemory.length;i++)
		{
			secondaryMemory[i]="";
			pageindex[i]=-1;
		}
		this.useLRU = useLRU;
		mainMemory= new String[50];
		if(useLRU)
		{
			index = new int[50][2];
			for(int i=0;i<index.length;i++)
			{
				index[i][0]=-1;
				index[i][1]=-1;	
			}
		}
		else
		{
			index = new int[50][1];
			for(int i=0;i<index.length;i++)
				index[i][0]=-1;
		}
		pointer=0;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		System.out.println( "secondaryMemory=" +Arrays.toString(secondaryMemory));
		return "secondaryMemory=" + Arrays.toString(secondaryMemory);
	}
	
	//This method returns the data you read. Notice that this data is not used by our main, but you can use it for testing your code.
	public String read(int index) {
		int i = getMainIndex(index);
		 if(i>-1)
		 {
			 if(useLRU)
				{
					pointer++;
					this.index[i][1]=pointer;
					resetAges();
				}
			 return mainMemory[this.index[i][0]];
		 }
		 else
		 {
			 load(index);
			 return mainMemory[0];
		 }
	}
	private void resetAges()
	{
		if (pointer == 2147483050)
		{
			for(int i=0;i<index.length;i++)
				index[i][1]=2147483000-index[i][1];
		}
	}
	public void write(int index, char c) {
		int i = getMainIndex(index); 
		if(i>-1)
		{
				mainMemory[i]=mainMemory[i]+c;
				if(useLRU)
				{
					pointer++;
					this.index[i][1]=pointer;
					resetAges();
						
				}
		}
		else
		{
			load(index);
			write(index,c);
		}
	}
	private int getMainIndex(int index)
	{
		
		/*for(int i=0;i<this.index.length;i++)
		{			
			if(this.index[i][0]==index)
				return i;
		}
		return -1;
		*/
		return pageindex[index];
	}
	private void flush(int index)
	{
		secondaryMemory[this.index[index][0]]=this.mainMemory[index];
	}
	private int getOldest()
	{
		int min=index[0][1];
		int ans=0;
		for(int i=1;i<index.length;i++)
		{
			if(index[i][1]<min)
			{
				min=index[i][1];
				ans=i;
			}
		}
		return ans;
	}
	private void load(int index)
	{
		if(useLRU)
		{
			int oldest;
			pointer++;
			oldest=getOldest();
			if(mainMemory[oldest]!=null)
				flush(oldest);
			this.index[oldest][0]=index;
			this.index[oldest][1]=pointer;
			pageindex[index]=oldest;
			mainMemory[oldest]=secondaryMemory[index];
		}
		else
		{
			if(mainMemory[pointer]!=null)
				flush(pointer);
			this.index[pointer][0]=index;
			mainMemory[pointer]=secondaryMemory[index];
			pageindex[index]=pointer;
			pointer=(pointer+1)%50;
		}
	}
}
